package com.jushu.storbox.view.viewImpl;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.widget.CompoundButton;

import com.jushu.storbox.R;
import com.jushu.storbox.util.PublicTools;

/**
 * 
 * Pin Progress Button
 */
public class PinProgressButton extends CompoundButton {

	private int mMax;
	private int mProgress;
	private Drawable mUnpinnedDrawable;
	private Drawable mPinnedDrawable;
	private Paint mCirclePaint;
	private Paint mProgressPaint;
	private Rect mTempRect = new Rect();
	private RectF mTempRectF = new RectF();
	private int mDrawableSize;
	private int mInnerSize;

	/**
	 * PinProgressButton Controller function
	 * 
	 * @param context
	 *            Context
	 */
	public PinProgressButton(Context context) {
		super(context);
		init(context, null, 0);
	}

	/**
	 * PinProgressButton Controller function
	 * 
	 * @param context
	 *            Context
	 * @param attrs
	 *            AttributeSet
	 */
	public PinProgressButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context, attrs, 0);
	}

	/**
	 * PinProgressButton Controller function
	 * 
	 * @param context
	 *            Context
	 * @param attrs
	 *            AttributeSet
	 * @param defStyle
	 *            int
	 */
	public PinProgressButton(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context, attrs, defStyle);
	}

	/**
	 * PinProgressButton init
	 * 
	 * @param context
	 *            Context
	 * @param attrs
	 *            AttributeSet
	 * @param defStyle
	 *            int
	 */
	private void init(Context context, AttributeSet attrs, int defStyle) {
		// mMax = 100;
		mProgress = 0;
		final Resources res = getResources();
		int circleColor = res.getColor(R.color.pin_progress_default_circle_color);
		int progressColor = res.getColor(R.color.pin_progress_default_progress_color);

		if (attrs != null) {
			// Attribute initialization
			final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PinProgressButton, defStyle, 0);
			mMax = a.getInteger(R.styleable.PinProgressButton_max, mMax);
			mProgress = a.getInteger(R.styleable.PinProgressButton_progress, mProgress);
			circleColor = a.getColor(R.styleable.PinProgressButton_circleColor, circleColor);
			progressColor = a.getColor(R.styleable.PinProgressButton_progressColor, progressColor);
			a.recycle();
		}
		DisplayMetrics dm = new DisplayMetrics();
		dm = getResources().getDisplayMetrics();
		int screenWidth = dm.widthPixels;
		int screenHeight = dm.heightPixels;
		// Other initialization
		mPinnedDrawable = res.getDrawable(R.mipmap.pin_progress_pinned);
		mPinnedDrawable.setCallback(this);
		mUnpinnedDrawable = res.getDrawable(R.mipmap.pin_progress_pinned);
		mUnpinnedDrawable.setCallback(this);

		mDrawableSize = PublicTools.dipTopx(getContext(), 16);
		// mInnerSize =
		// res.getDimensionPixelSize(R.dimen.pin_progress_inner_size);
		mInnerSize = PublicTools.dipTopx(getContext(), 15);

		mCirclePaint = new Paint();
		mCirclePaint.setColor(circleColor);
		mCirclePaint.setAntiAlias(true);

		mProgressPaint = new Paint();
		mProgressPaint.setColor(progressColor);
		mProgressPaint.setAntiAlias(true);
	}

	/**
	 * @TODO
	 * @return Returns the maximum download progress value.
	 */
	public int getMax() {
		return mMax;
	}

	/**
	 * @return Sets the maximum download progress value. Defaults to 100.
	 */
	public void setMax(int max) {
		mMax = max;
		invalidate();
	}

	/**
	 * @return Returns the current download progress from 0 to max.
	 */
	public int getProgress() {
		return mProgress;
	}

	/**
	 * @return Sets the current download progress (between 0 and max).
	 * @see #setMax(int)
	 */
	public void setProgress(int progress) {
		mProgress = progress;
		invalidate();
	}

	/**
	 * PinProgressButton onMeasure
	 * 
	 * @param widthMeasureSpec
	 *            int
	 * @param heightMeasureSpec
	 *            int
	 */
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(resolveSize(mDrawableSize, widthMeasureSpec), resolveSize(mDrawableSize, heightMeasureSpec));
	}

	/**
	 * PinProgressButton drawableStateChanged
	 */
	@Override
	protected void drawableStateChanged() {
		super.drawableStateChanged();
		if (mPinnedDrawable.isStateful()) {
			mPinnedDrawable.setState(getDrawableState());
		}
		if (mUnpinnedDrawable.isStateful()) {
			mUnpinnedDrawable.setState(getDrawableState());
		}
	}

	/**
	 * PinProgressButton onDraw
	 * 
	 * @param canvas
	 *            Canvas
	 */
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		mTempRect.set(0, 0, mDrawableSize, mDrawableSize);
		mTempRect.offset((getWidth() - mDrawableSize) / 2, (getHeight() - mDrawableSize) / 2);

		mTempRectF.set(-0.5f, -0.5f, mInnerSize + 0.5f, mInnerSize + 0.5f);
		mTempRectF.offset((getWidth() - mInnerSize) / 2, (getHeight() - mInnerSize) / 2);

		canvas.drawArc(mTempRectF, 0, 360, true, mCirclePaint);
		canvas.drawArc(mTempRectF, -90, 360 * mProgress / mMax, true, mProgressPaint);

		Drawable iconDrawable = isChecked() ? mPinnedDrawable : mUnpinnedDrawable;
		iconDrawable.setBounds(mTempRect);
		iconDrawable.draw(canvas);

	}

	/**
	 * A {@link Parcelable} representing the {@link PinProgressButton}'s state.
	 */
	public static class SavedState extends BaseSavedState {
		private int mProgress;
		private int mMax;

		public SavedState(Parcelable superState) {
			super(superState);
		}

		private SavedState(Parcel in) {
			super(in);
			mProgress = in.readInt();
			mMax = in.readInt();
		}

		@Override
		public void writeToParcel(Parcel out, int flags) {
			super.writeToParcel(out, flags);
			out.writeInt(mProgress);
			out.writeInt(mMax);
		}

		public static final Parcelable.Creator<SavedState> CREATOR = new Creator<SavedState>() {
			@Override
			public SavedState createFromParcel(Parcel parcel) {
				return new SavedState(parcel);
			}

			@Override
			public SavedState[] newArray(int size) {
				return new SavedState[size];
			}
		};

	}

	/**
	 * SavedState onSaveInstanceState
	 * 
	 * @return Parcelable
	 */
	@Override
	public Parcelable onSaveInstanceState() {
		Parcelable superState = super.onSaveInstanceState();
		if (isSaveEnabled()) {
			SavedState ss = new SavedState(superState);
			ss.mMax = mMax;
			ss.mProgress = mProgress;
			return ss;
		}
		return superState;
	}

	/**
	 * SavedState onRestoreInstanceState
	 * 
	 * @return Parcelable
	 */
	@Override
	public void onRestoreInstanceState(Parcelable state) {
		if (!(state instanceof SavedState)) {
			super.onRestoreInstanceState(state);
			return;
		}
		SavedState ss = (SavedState) state;
		super.onRestoreInstanceState(ss.getSuperState());

		mMax = ss.mMax;
		mProgress = ss.mProgress;
	}
}
